package mo.leetcode_top;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class L131 {

    public List<List<String>> partition(String s) {
        List<List<String>> res = new ArrayList<>();
        LinkedList<String> out = new LinkedList<>();
        helper(s, 0, out, res);
        return res;
    }

    private void helper(String s, int start, LinkedList<String> out, List<List<String>> res) {
        if (start == s.length()) {
            res.add(new ArrayList<>(out));
        }
        for (int i = start; i < s.length(); i++) {
            if (!isPalindrome(s, start, i)) {
                continue;
            }
            out.add(s.substring(start, i+1));
            helper(s, i+1, out, res);
            out.removeLast();
        }
    }

    private boolean isPalindrome(String s, int start, int end) {
        while (start < end) {
            if (s.charAt(start) != s.charAt(end)) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }

    public static void main(String[] args) {
        L131 test = new L131();
        System.out.println(test.partition("a"));
    }

}
